package com.example.yls.demoa;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;

import java.util.ArrayList;

import javax.security.auth.callback.Callback;

/**
 * Created by yls on 2018/10/13.
 */

public class StuAdapter extends BaseAdapter{
    private Context context;
    private ArrayList<Student> studentArrayList;
    private IOnDelListenner listenner;
    //private DeleteCallback Callback;
    //private int delItemId;

    public StuAdapter(Context context,ArrayList<Student> studentArrayList, IOnDelListenner listenner){
        this.context = context;
        this.studentArrayList = studentArrayList;
        this.listenner = listenner;
    }

    public void changeData(ArrayList<Student> studentArrayList){
        this.studentArrayList = studentArrayList;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return studentArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if(view == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            view = layoutInflater.inflate(R.layout.my_list_item,
                    viewGroup,false);

            StuViewHolder holder = new StuViewHolder();
            holder.headView = view.findViewById(R.id.headId_view);
            holder.nameView = view.findViewById(R.id.name_view);
            holder.ageView = view.findViewById(R.id.agee_view);
            holder.delView = view.findViewById(R.id.del_view);

            view.setTag(holder);
        }


        Student student = studentArrayList.get(i);
        final StuViewHolder holder = (StuViewHolder)view.getTag();

        holder.headView.setImageResource(student.getHeadId());
        holder.nameView.setText(student.getName());
        holder.ageView.setText(String.valueOf(student.getAge()));

        /*
        * 测试删除学生
        * */
        holder.delView.setTag(i);
        holder.delView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //https://blog.csdn.net/anroidyanyou/article/details/51273327
                listenner.delIndex(studentArrayList.get(i).getNo());
            }
        });
        /*
        * 测试删除学生
        *
        * */
        return view;
    }



    //以下
    /*public interface DeleteCallback {
        void deletePosition(int delItemId);
    }
    public void setDeleteCallback(DeleteCallback Callback){
        this.Callback=Callback;
    }
*/
}
